package com.fudan.sys.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.fudan.framework.base.BaseDao;
import com.fudan.sys.entity.User;

@Repository
public interface UserDao extends BaseDao<User> {

    public User findByUsername(String username);

    @Query(nativeQuery = true, value = "select u.*,r.name as roleName,r.discard as roleNameDiscard "
            + "from t_user u left join t_role r on u.role_id=r.id "
            + "where u.id=?1")
    public Map<String, Object> getUserInfo(Integer id);


    @Query(nativeQuery = true, value = "select u.id as id,u.username as name,r.name as roleName,r.discard as roleNameDiscard,u.comment as comment "
            + "from t_user u left join t_role r on u.role_id=r.id "
            + "where u.discard=0 limit :from,:to")
    public List<Map<String, Object>> getUserList(@Param("from") int from, @Param("to") int to);

	@Query(nativeQuery=true,value="select count(*) "
			+ "from t_user u left join t_role r on u.role_id=r.id "
			+ "where u.discard=0")
	public Integer countUser();


	@Query(nativeQuery=true,value="select u.id as id,u.username as name,r.name as roleName,u.comment as comment "
			+ "from t_user u left join t_role r on u.role_id=r.id where u.username like %:name% limit :from,:to")
	public List<Map<String,Object>> getUserListByUserName(@Param("name")String name,@Param("from")int from,@Param("to")int to);

	@Query(nativeQuery = true,value = "SELECT user.id as id,user.username as userName,user.comment as comment,user.type as type,user.status as status,token.user_token as userToken\n" +
			"FROM t_user as user\n" +
			"LEFT JOIN t_user_token as token on user.id = token.user_id  limit :from,:size")
	public List<Map<String,Object>>getUserAndTokenList(@Param("from") int from, @Param("size") int size);
}
